package com.kly.cashmall.module.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kly.cashmall.R;
import com.kly.cashmall.base.BaseBarActivity;
import com.kly.cashmall.bean.MessageBean;
import com.kly.cashmall.framework.mvp.PresenterLifeCycle;
import com.kly.cashmall.module.mine.adapter.MessageAdapter;
import com.kly.cashmall.module.mine.presenter.MessagePresenter;
import com.kly.cashmall.module.mine.presenter.MessageViewer;
import com.kly.cashmall.utils.data.CollectionUtils;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;


public class MessageActivity extends BaseBarActivity implements MessageViewer {

    @PresenterLifeCycle
    MessagePresenter presenter = new MessagePresenter(this);
    private int pageNo = 1;
    private RecyclerView mNoticeList;
    private MessageAdapter mAdapter;
    private SmartRefreshLayout mNoticeListRefresh;

    public static Intent newIntent(Context context){
        return new Intent(context,MessageActivity.class);
    }
    @Override
    protected void setView(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_message);
        setTitle("Notice");
        initView();
        initEvent();
    }

    private void initEvent() {
        mNoticeListRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNo++;
                presenter.getMessageList(pageNo);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNo = 1;
                presenter.getMessageList(pageNo);
            }
        });
    }

    private void initView() {
        mNoticeListRefresh = bindView(R.id.notice_list_refresh);
        mNoticeList = bindView(R.id.notice_list);
        mNoticeList.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MessageAdapter();
        mNoticeList.setAdapter(mAdapter);
    }

    @Override
    protected void loadData() {
        presenter.getMessageList(pageNo);
    }


    @Override
    public void getMessageListRefreshSuccess(MessageBean noticeBean) {
        if (noticeBean != null) {
            bindView(R.id.notice_list_refresh,true);
            if (mNoticeListRefresh.getState() == RefreshState.Refreshing) {
                mNoticeListRefresh.finishRefresh();
            }
            if (CollectionUtils.isEmpty(noticeBean.getMessages())){
                bindView(R.id.notice_list_refresh,false);
                bindView(R.id.status_empty_view,true);
                bindText(R.id.empty_title, getString(R.string.no_record));
                ((ImageView) bindView(R.id.empty_inc)).setImageResource(R.mipmap.ic_list_none);
            }else {
                bindView(R.id.notice_list_refresh,true);
                bindView(R.id.status_empty_view,false);
                mNoticeListRefresh.setEnableLoadMore(noticeBean.isHasNextPage());
                mAdapter.setList(noticeBean.getMessages());
            }
        }
    }

    @Override
    public void getMessageListLoadMoreSuccess(MessageBean noticeBean) {
        if (noticeBean != null) {
            if (mNoticeListRefresh.getState() == RefreshState.Loading) {
                mNoticeListRefresh.finishLoadMore();
            }
            mNoticeListRefresh.setEnableLoadMore(noticeBean.isHasNextPage());
            mAdapter.addData(noticeBean.getMessages());
        }
    }
}
